#include <stdio.h>
#include <time.h>

char *wday_name[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};


void put_date(const struct tm *timer)
{
    printf(
            "%4d-%02d-%02d (%3s)%02d:%02d:%02d",
            timer->tm_year + 1900,
            timer->tm_mon + 1,
            timer->tm_mday,
            wday_name[timer->tm_wday],
            timer->tm_hour,
            timer->tm_min,
            timer->tm_sec
            );
}

int dayofweek(int y, int m, int d)
{
    struct tm t;
    t.tm_year = y - 1900;
    t.tm_mon = m -1;
    t.tm_mday = d;
    t.tm_hour = 0;
    t.tm_min = 0;
    t.tm_sec = 0;
    t.tm_isdst = -1;

    if (mktime(&t) == (time_t)-1) {
        return -1;

    }

    put_date(&t);

    return t.tm_wday;
}

void main(void){
    time_t current = time(NULL);

    printf("%lu sizeof time_t %lu \n", current, sizeof(time_t));

    struct tm *timer = localtime(&current);

    puts("\ncurrent is:");

    put_date(timer);

    putchar('\n');


    puts("\nUTC:");
    timer = gmtime(&current);
    put_date(timer);
    putchar('\n');

    printf("\nasctime\n %s",asctime(localtime(&current)));

    printf("\n\nctime\n %s", ctime(&current));

    printf("\n\nmktime\n %s", "2018/8/13日是星期几?");

    int w = dayofweek(2018, 8, 13);

    printf("是 %s", wday_name[w]);

}


